package com.zj.study;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
public class MyTransactionListener {
//spring的事件监听（三元素：ApplicationEvent，@TransactionalEventListener，ApplicationEventPublisher）和
// 容器监听（三元素：ApplicationEvent，@EventListener，ApplicationEventPublisher）基本是一样的，
// 区别在于TransactionalEventListener在EventListener基础上多了几个条件

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    @Order(10)
    public void dosomething(MyApplicationEvent event){
        System.out.println(event.getSource()+":source:"+Thread.currentThread().getName());
    }

    @TransactionalEventListener(classes =MyApplicationEvent.class )
    @Order(2)
    //有多个listener时可以通过注解@Order去排序所有的Listener，确保他们按自己的设定的预期顺序执行。
    public String dootherthing(MyApplicationEvent event){
        System.out.println(event.getSource()+":source:"+Thread.currentThread().getName());
        return "hehe";
    }
}
